package com.chen.pspms.service.impl;

import com.chen.pspms.entity.CompMajorStatistics;
import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.mapper.MajorhazardregisterMapper;
import com.chen.pspms.service.CompMajorStatisticsService;
import com.chen.pspms.utils.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompMajorStatisticsServiceImpl implements CompMajorStatisticsService {
    @Autowired
    MajorhazardregisterMapper majorhazardregisterMapper;

    @Override
    public CompMajorStatistics getByCompany(CompanyInfo company) throws ApplicationException {
        //
        CompMajorStatistics compMajorStatistics=null;
        try {
            List<Majorhazardregistertable> majorhazardregisterList = majorhazardregisterMapper.findyListByComId(company.getId());
            Integer inspectionNum = majorhazardregisterList.size();
            //循环，查找无的数量
            Integer warningNum=0,level1Num=0,level2Num=0,level3Num=0;
            for(Majorhazardregistertable majorhazardregistertable:majorhazardregisterList){
                switch (majorhazardregistertable.getLevel()){
                    case "预警":warningNum++;break;
                    case "一级":level1Num++;break;
                    case "二级":level2Num++;break;
                    case "三级":level3Num++;break;
                    default:break;
                }
            }
            //创建CompMajorStatistics
            compMajorStatistics=new CompMajorStatistics(company,inspectionNum,warningNum,level1Num,level2Num,level3Num);
        } catch (Exception e) {
            throw new ApplicationException("新增企业信息失败", e);
        }
        return compMajorStatistics;
    }

    @Override
    public List<CompMajorStatistics> getListByCompany(List<CompanyInfo> companylist) throws ApplicationException {
        List<CompMajorStatistics> compMajorStatisticsList=new ArrayList<>();
        for(CompanyInfo company:companylist){
            CompMajorStatistics compMajorStatistics = getByCompany(company);
            compMajorStatisticsList.add(compMajorStatistics);
        }
        return compMajorStatisticsList;
    }
}
